package v1

import (
	"auth/bll"
	"auth/collector/logger"
	"auth/errors"
	"auth/model"
	"auth/store/postgres"
	"context"
	"gorm.io/gorm"
	"proto/dto"
)

type User struct{}

func (*User) UserInfo(ctx context.Context, in *dto.UserInfoRequest, out *dto.UserInfoResponse) error {
	var (
		err error
	)

	// 查询用户信息
	userInfo := &model.UserInfo{
		ID:    in.Id,
		Phone: in.Phone,
	}
	outInfo, err := bll.User.FindByParameter(userInfo)
	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
		logger.Error(err)
		return err
	}

	*out = dto.UserInfoResponse{
		Id:      outInfo.ID,
		Phone:   outInfo.Phone,
		Name:    outInfo.Name,
		Picture: outInfo.Picture,
		Status:  int32(outInfo.Status),
	}
	return nil
}

func (*User) UserListInfo(ctx context.Context, in *dto.UserListInfoRequest, out *dto.UserListInfoResponse) error {

	ou, err := bll.User.UserListByIds(ctx, dto.UserListInfoRequest{Ids: in.Ids})
	*out = ou
	return err
}

// AllUserIds 获取所有用户ID
func (*User) AllUserIds(ctx context.Context, in *dto.AllUserIdsRequest, out *dto.AllUserIdsResponse) error {
	var err error
	*out = dto.AllUserIdsResponse{}
	out.Ids, err = postgres.User.AllUserIds()
	return err
}
